package com.winchain.chainbill.bill.biz.repurchase.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.governor.modules.message.keyword.RpurRejectKeyword;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.biz.Bill;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRpurRjMsgCmd implements ICommand<Bill<CbRpur>>, ICallback<Bill<CbRpur>, AcctTxRec> {
	@Autowired
	CusRecService cusRecService;
	@Autowired
	CbDctService cbDctService;
	@Autowired
	CbRecService cbRecService;

	public boolean execute(CommandContext<Bill<CbRpur>> context) throws Exception {
		CbRpur cbRpur = (CbRpur) ((Bill) context.getSource()).getSource();
		CbOpt cbOpt = (CbOpt) context.getValue("Operation");

		String template = "MT02617";
		RpurRejectKeyword bKeyWord = new RpurRejectKeyword();
		bKeyWord.setBillAmt(String.valueOf(cbRpur.getRpurAmt()));
		bKeyWord.setRpurDate(DateUtil.parseDate(cbRpur.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
		bKeyWord.setRemark(cbOpt.getRemark());

		CbRec cbRec = new CbRec();
		cbRec.setBillId(cbRpur.getBillId());
		List<CbRec> list = this.cbRecService.selectByEntity(cbRec);
		if (list.size() > 0) {
			bKeyWord.setBillCode(((CbRec) list.get(0)).getBillCode());
		}
		String senderId = CommonDataUtil.getUserId();
		String buyerId = cbRpur.getBuyerId();

		MessageSender.sendMessageByTmpl(senderId, template, bKeyWord, buyerId);
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRpur>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
